package com.it.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.Mapper.OrderMapper;
import com.it.Service.OrderService;
import com.it.pojo.Flight;
import com.it.pojo.Order;
import com.it.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageBean OList(String idNum, Integer page, Integer pageSize) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);

        //2.执行查询
        List<Order> orderList = orderMapper.OList(idNum);
        Page<Order> p=(Page<Order>)orderList;

        //封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    //订单编号生成器
    private static String generateOrderNumber() {
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();
        // 组合生成订单编号
        return "ORD" + timestamp;
    }

    @Override
    public void OAdd(List<Order> orders) {
        String orderNumber = generateOrderNumber();
        LocalDateTime currentTime = LocalDateTime.now();
        for(int i=0;i<orders.size();i++){
            Order order = orders.get(i);
            order.setONumber(orderNumber);
            order.setOTime(currentTime);
            if(i==0){//避免往order中插入重复主键订单编号
                orderMapper.OAdd1(order);
            }
            orderMapper.OAdd2(order);
        }
    }

    @Override
    public void reduceTickets(Order order) {
        orderMapper.reduceTickets(order);
    }

    @Override
    public void ORefund(String oNumber) {
        orderMapper.ORefund(oNumber);
    }

    @Override
    public void increaseTickets(String oNumber) {
        orderMapper.increaseTickets(oNumber);
    }

    @Override
    public List<String> getFNumByIdNum(String idNumber) {
        return orderMapper.getFNumByIdNum(idNumber);
    }

    @Override
    public List<String> getFNumByONum(String oNumber) {
        return orderMapper.getFNumByONum(oNumber);
    }

    @Override
    public void ORebook(String oNumber) {
        orderMapper.ORebook(oNumber);
    }

    @Override
    public void ODelete(String oNumber) {
        orderMapper.ODelete(oNumber);
    }

    @Override
    public List<Order> getOrderById(String oNumber) {
        return orderMapper.getOrderById(oNumber);
    }
}
